﻿using System;
using System.Xml;
using System.Xml.Serialization;

namespace GISUtil.KML
{
  /// <summary>
  /// Contains methods for serializing and deserializing the XML found in the HotSpot section of KML.
  /// </summary>
  [XmlRoot("hotSpot")]
  public class HotSpot
  {
    /// <summary>
    /// The value of the X offset.
    /// </summary>
    [XmlAttribute("x")]
    public string X { get; set; }

    /// <summary>
    /// The value of the Y offset.
    /// </summary>
    [XmlAttribute("y")]
    public string Y { get; set; }

    /// <summary>
    /// The way in which the X value is offset.  
    /// </summary>
    [XmlIgnore]
    public HotSpotUnit? XUnits { get; set; }

    [XmlAttribute("xunits")]
    public string XUnitsString
    {
      get
      {
        if (XUnits.HasValue)
        {
          return XUnits.Value.ToString();
        }
        return null;
      }
      set
      {
        HotSpotUnit unit;
        if (Enum.TryParse<HotSpotUnit>(value, out unit))
        {
          XUnits = unit;
        }
        else
        {
          XUnits = null;
        }
      }
    }

    /// <summary>
    /// The way in which the Y value is offset.  
    /// </summary>
    [XmlIgnore]
    public HotSpotUnit? YUnits { get; set; }

    [XmlAttribute("yunits")]
    public string YUnitsString
    {
      get
      {
        if (YUnits.HasValue)
        {
          return YUnits.Value.ToString();
        }
        return null;
      }
      set
      {
        HotSpotUnit unit;
        if (Enum.TryParse<HotSpotUnit>(value, out unit))
        {
          YUnits = unit;
        }
        else
        {
          YUnits = null;
        }
      }
    }

    /// <summary>
    /// Determines if this class has any styling information associated with it. 
    /// </summary>
    /// <returns>True if at least one of the fields that contains style information has a value, otherwise false.</returns>
    public bool HasAValue()
    {
      return X != null || Y != null || XUnits.HasValue || YUnits.HasValue;
    }
  }
}